#!/bin/bash

CONFIG="$( cd "$( dirname "$0" )" && pwd )"
CONFIG="$CONFIG/rbl-reinstall.rc"

if [ ! -f $CONFIG ]; then
    echo "$CONFIG does not exist"
    exit
fi

source $CONFIG

if [ -z "$VERSION" ]; then
    echo "VERSION not set"
    exit
fi

if [ -z "$DBROOTPW" ]; then
    echo "DBROOTPW not set"
    exit
fi
if [ -z "$DBPW" ]; then
    echo "DBPW not set"
    exit
fi
if [ ! -d ~/redmine ]; then
    echo "~/redmine does not exist"
    exit
fi

cd ~/redmine

if [ ! -f ~/redmine/setup/database.yml ]; then
    echo "~/redmine/setup/database.yml does not exist"
    exit
fi

if [ ! -d ~/redmine/redmine_backlogs ]; then
    echo "~/redmine/redmine_backlogs does not exist"
    exit
fi

# ruby env
if [ -z "$RVM" ]; then
  read -p "Reinstall RVM? " RVM
fi
if [[ $RVM =~ [Yy] ]]; then
    #RUBYVER=1.8.7
    #echo "Installing ruby $RUBYVER"
    #rvm use system
    #rvm remove $RUBYVER
    #rvm install $RUBYVER
    #rvm use --default $RUBYVER
    #echo 'Ruby installation done'
    rvm --force gemset empty global

    echo 'Downgrading rubygems'
    rvm rubygems 1.3.7 

    echo 'Installing base gems'
    rvm gem install mysql
    rvm gem install rack -v=1.0.1
    rvm gem install rails -v=2.3.5
    rvm gem install rmagick
    rvm gem install -v=0.4.2 i18n
    rvm gem install icalendar
    rvm gem install prawn
    rvm gem install holidays
fi

# redmine
if [ ! -f ~/redmine/redmine/config/database.yml ]; then
  echo Forced RM install
  RM=Y
elif [ -z "$RM" ]; then
  read -p "Reinstall Redmine? " RM
fi
if [[ $RM =~ [Yy] ]]; then
    rm -rf ~/redmine/redmine
    cd ~/redmine
    git clone git://github.com/edavis10/redmine.git
    cd ~/redmine/redmine
    git fetch --tags
    git checkout $VERSION
    cp ~/redmine/setup/database.yml ~/redmine/redmine/config
    ln -s ~/redmine/redmine_backlogs ~/redmine/redmine/vendor/plugins
    ln -s ~/redmine/redmine_backlogs/features ~/redmine/redmine
fi

# dev
if [ -z "$DEV" ]; then
  read -p "Dev environment? " DEV
fi
if [[ $DEV =~ [Yy] ]]; then

    rvm gem install -v=1.3.1 rspec
    rvm gem install -v=0.8.5 cucumber
    rvm gem install spork
    rvm gem install database_cleaner
    rvm gem install capybara
    rvm gem install ZenTest
    rvm gem install autotest-rails
    rvm gem install redgreen
    rvm gem install rcov
    rvm gem install -v=1.3.3 rspec-rails
    rvm gem install -v=0.3.2 cucumber-rails

    # cucumber-rails pulls in an unnecesary gherkin
    rvm gem uninstall gherkin -v=2.3.5

    cd ~/redmine/redmine
    [ -f ~/redmine/redmine/config/environments/cucumber.rb ] || script/generate cucumber --spork
fi

# database
if [ -z "$DB" ]; then
  read -p "Reinstall database? " DB
fi
if [[ $DB =~ [Yy] ]]; then
    mysqladmin -u root -p$DBROOTPW drop redmine
    echo "create database redmine character set utf8;" | mysql -u root -p$DBROOTPW mysql
    echo "create user 'redmine'@'localhost' identified by '$DBPW';" | mysql -u root -p$DBROOTPW mysql
    echo "grant all privileges on redmine.* to 'redmine'@'localhost';" | mysql -u root -p$DBROOTPW mysql

    cd ~/redmine/redmine
    rake generate_session_store
    rake db:migrate
    rake redmine:load_default_data
    rake db:migrate:upgrade_plugin_migrations
    rake db:migrate_plugins
    rake redmine:backlogs:install
fi
